# -*- coding: utf-8 -*-
# @Time : 2022-07-12 15:26
# @Author : Z
# @Function : 图片上传

from selenium import webdriver  # 从selenium库中导入webdriver包
from time import sleep
from selenium.webdriver.common.by import By
from Common.Logger import path
import pywinauto

driver = webdriver.Chrome()
url = 'https://www.baidu.com/'
driver.get(url)
driver.maximize_window()
# # 点击搜索图片
# ele =driver.find_element(By.XPATH,'//*[@id="kw"]/../span[@class="soutu-btn"]')
# ele.click()
# # driver.execute_script('arguments[0].click()',ele)
# sleep(3)

# send_keys()上传图片
# element = driver.find_element(By.CLASS_NAME,'upload-pic')  # 定位到上传文件框
# element.send_keys(path+r'\lib\imgs\test.png')  # 上传文件

# pywinauto上传图片
# pywinauto模拟操作键盘，需要使用到 pywinauto.keyboard.send_keys这个方法
# 需要注意的是Pywinauto库的send_keys方法与selenium库的send_keys方法不同，输入的键盘操作关键字存在较大差异
from pywinauto.keyboard import send_keys

# js点击
ele1 = driver.find_element(By.XPATH,'//input[@value="上传图片"]')
driver.execute_script('arguments[0].click()',ele1)
driver.find_element(By.XPATH,'//*[@id="form"]/div/div[2]/div[2]').click()

# sleep(2)


# 使用pyautoit创建操作桌面窗口对象
win1 = pywinauto.Desktop()

# 选择文件上传窗口，窗口句柄默认打开
bow1 = win1['打开']

# 选择文件地址输入框，点击激活
bow1['Toolbar3'].click()

# 键盘输入文件路径
send_keys(path + r'\lib\imgs')

# 键盘输入回车，打开该路径
send_keys("{VK_RETURN}")

# 选中文件名输入框，输入文件名
bow1['文件名(&N):Edit'].type_keys('test.png')

# 点击打开
bow1["打开(&O)"].click()

sleep(3)
driver.quit()
